import 'package:flutter_nb_net/flutter_net.dart';
import 'package:tracker/common/log/x_logger.dart';
import 'package:tracker/pages/home/track/model/reverse_geocoding_resp.dart';

class DeviceUtils {
  /// 获取设备状态文本
  static String getStatusText(int? deviceStatus) {
    if (deviceStatus == null) {
      return '';
    }
    if (deviceStatus == 1) {
      return '在线';
    } else if (deviceStatus == 2) {
      return '离线';
    } else if (deviceStatus == 3) {
      return '深度休眠';
    }
    return '远程关机';
  }

  /// 获取充电状态文本
  static String getChargingStatusText(int? chargingStatus) {
    if (chargingStatus == null) {
      return '';
    }
    if (chargingStatus == 0) {
      return '充电中';
    } else if (chargingStatus == 1) {
      return '未充电';
    }
    return '';
  }

  /// 逆地址解析
  static Future<String> reverseGeoCode({required double latitude, required double longitude, String? gpsDataId}) async {
    String locationDesc = '';
    final res = await post<ReverseGeocodingResp, ReverseGeocodingResp>('/track/reverseGeocoding',
        data: {
          'latitude': latitude,
          'longitude': longitude,
          'gpsDataId': gpsDataId,
        },
        decodeType: ReverseGeocodingResp());
    res.when(success: (resp) {
      if (resp.code == 200) {
        if (resp.data != null) {
          locationDesc = resp.data!.locationDesc ?? '';
        }
      }
    }, failure: (String msg, int code) {
      XLogger.error('逆地址解析失败：msg: $msg, code: $code');
    });
    return locationDesc;
  }
}
